package perform.query.impl;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import perform.exception.QueryException;
import perform.mappings.ClassMappings;
import perform.query.AbstractQuery;
import perform.query.builder.QueryBuilder;
import perform.type.Type;

public class InsertQuery extends AbstractQuery {
	
	public InsertQuery(Type type, Connection connection) throws QueryException{
		super();
		try {
			query = QueryBuilder.getInsertQuery(type.getClass());
			preparedStatement = connection.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
			Map<Field, String> allColumns = ClassMappings.getAllColumns(type.getClass());
			List<Field> fields = new ArrayList<Field>(allColumns.keySet());
			for(int i = 0; i < fields.size(); i++){
				setValue(i + 1, fields.get(i).get(type));
			}
		} catch (Exception e) {
			throw new QueryException(e);
		}
	}

}
